package com.nny.alarm.edit;

import android.content.Context;
import android.text.format.Time;

import com.nny.alarm.edit.adapter.DateAdapter;
import com.nny.alarm.edit.adapter.HourAdapter;
import com.nny.alarm.edit.adapter.MinuteAdapter;
import com.nny.alarm.edit.adapter.MonthAdapter;
import com.nny.alarm.edit.adapter.YearAdapter;
import com.nny.alarm.widget.DialView;
import com.nny.alarm.widget.TimeView;
import com.uxwine.alarm.Alarm;
import com.uxwine.alarm.AlarmCalc;
import com.uxwine.alarm.TimeCycle2;
import com.uxwine.alarm.TimeRecord;
import com.uxwine.util.UxToast;

public class TimeEditor implements IAlarmEditor
{
	AlarmEditor mEditor;
	DialAdapter mAdapter;
	Context mCtx;
	
	public TimeEditor(AlarmEditor a)
	{
		mEditor = a;
		mCtx = a.mCtx;
	}

	@Override
	public void edit()
	{
		final TimeView dttv = mEditor.mDigital;
		final DialView dtv = mEditor.mDial;
		
		//更新数字钟界面：包括设置mark,设置enable和设置时间		
		dttv.update(mEditor.mAlarm);
		dttv.updateDigitals();
		
		mAdapter = createAdapter(dttv.first(), mEditor.mAlarm);
		dtv.setAdapter(mAdapter);
		mAdapter.notifyDataSetChanged();
	}

	@Override
	public boolean next()
	{
		final TimeView dttv = mEditor.mDigital;
		editByKey(dttv.next());
		return true;
	}

	@Override
	public void onMarked(int index)
	{
		final TimeView dttv = mEditor.mDigital;
		dttv.set(dttv.current(), (int)mAdapter.getValue(index));		
	}

	@Override
	public void onMarkChange(int index)
	{
		final TimeView dttv = mEditor.mDigital;
		dttv.set(dttv.current(), (int)mAdapter.getValue(index));
	}	
	
	public void edit(int key)
	{
		final TimeView dttv = mEditor.mDigital;
		dttv.setStep(key);
		editByKey(key);
	}
	
	private void editByKey(int key)
	{
		final DialView dtv = mEditor.mDial;
		final TimeView dttv = mEditor.mDigital;
		if(TimeRecord.DATE == key)
		{
			mAdapter = createAdapter(key, mEditor.mAlarm);
			dtv.setAdapter(mAdapter);
			mAdapter.notifyDataSetChanged();
			
			dttv.set(key, (int)mAdapter.getValue(mAdapter.getPosition()));
		}
		else if(TimeRecord.SECOND == key)
		{
			mEditor.step(AlarmEditor.STEP_RING);
		}
		else
		{
			mAdapter = createAdapter(key, mEditor.mAlarm);
			dtv.setAdapter(mAdapter);
			mAdapter.notifyDataSetChanged();
		}
	}
	
	DialAdapter createAdapter(int key, Alarm a)
	{
		TimeRecord time = a.time;
		TimeCycle2 cycle = a.cycle;
		switch(key)
		{
		case TimeRecord.YEAR:
		{
			int year = time.get(TimeRecord.YEAR);
			return new YearAdapter(mCtx, year);
		}
		case TimeRecord.MONTH:
		{
			int month = time.get(TimeRecord.MONTH);
			return new MonthAdapter(mCtx, month);
			
		}
		case TimeRecord.DATE:
		{
			int day = time.get(TimeRecord.DATE);
			switch(cycle.getCycle())
			{
			case TimeCycle2.TYPE_MONTH:
			{
				return new DateAdapter(mCtx, day, 31);
			}
			case TimeCycle2.TYPE_YEAR:
			{
				int month = time.get(TimeRecord.MONTH) + 1;
				return new DateAdapter(mCtx, day, getDaysOfMonthMax(month));
			}
			case TimeCycle2.TYPE_ONCE:
			{
				int year = time.get(TimeRecord.YEAR);
				int month = time.get(TimeRecord.MONTH) + 1;
				return new DateAdapter(mCtx, day, getDaysOfMonth(year, month));
			}
			default: throw new IllegalArgumentException();
			}
		}
		case TimeRecord.HOUR:
		{
			int hour = time.get(TimeRecord.HOUR);
			return new HourAdapter(mCtx, hour);
		}
		case TimeRecord.MINUTE:
		{
			int minute = time.get(TimeRecord.MINUTE);
			return new MinuteAdapter(mCtx, minute);
		}
		default:
			throw new IllegalArgumentException();
		}
	}
	
	public int getDaysOfMonth(int year, int month)
	{
		if(month == 2)
			return (TimeCycle2.isLeap(year)) ? 29 : 28;
		return getDaysOfMonthMax(month);
	}
	
	public int getDaysOfMonthMax(int month)
	{
		switch(month)
		{
		case 1:
		case 3:
		case 5:
		case 7:
		case 8:
		case 10:
		case 12:
			return 31;
		case 4:
		case 6:
		case 9:
		case 11:
			return 30;
		case 2:
			return 29;
		}
		return -1;
	}

	@Override
	public void over()
	{
		
	}

	@Override
	public void reset()
	{
		
	}
}
